package org.jeecg.modules.exPingbiao.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.exPingbiao.entity.ExTbcompanyScore;
import org.jeecg.modules.exPingbiao.entity.ExZbcandidateDetailed;
import org.jeecg.modules.exPingbiao.service.IExTbcompanyScoreService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.exPingbiao.service.IExZbcandidateDetailedService;
import org.jeecg.modules.exPingbiao.vo.ExTbcompanyScoreCandidate;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exTender.entity.ExTenderCandidateAnnounce;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 投标单位得分
 * @Author: jeecg-boot
 * @Date: 2024-11-27
 * @Version: V1.0
 */
@Api(tags = "投标单位得分")
@RestController
@RequestMapping("/exPingbiao/exTbcompanyScore")
@Slf4j
public class ExTbcompanyScoreController extends JeecgController<ExTbcompanyScore, IExTbcompanyScoreService> {
    @Autowired
    private IExTbcompanyScoreService exTbcompanyScoreService;
    @Autowired
    private IExZbcandidateDetailedService exZbcandidateDetailedService;

    /**
     * 分页列表查询
     *
     * @param exTbcompanyScore
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "投标单位得分-分页列表查询")
    @ApiOperation(value = "投标单位得分-分页列表查询", notes = "投标单位得分-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ExTbcompanyScoreCandidate>> queryPageList(ExTbcompanyScore exTbcompanyScore,
                                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                  HttpServletRequest req) {
        QueryWrapper<ExTbcompanyScore> queryWrapper = QueryGenerator.initQueryWrapper(exTbcompanyScore, req.getParameterMap());
        Page<ExTbcompanyScore> page = new Page<ExTbcompanyScore>(pageNo, pageSize);
        IPage<ExTbcompanyScore> pageList = exTbcompanyScoreService.page(page, queryWrapper);
        IPage<ExTbcompanyScoreCandidate> page1 = new Page<>();
        BeanUtils.copyProperties(pageList, page1);
        List<String> tenderListId = pageList.getRecords().stream().map(ExTbcompanyScore::getTenderListId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(tenderListId)){
            return Result.error("没有找到对应的标段信息");
        }
        List<ExZbcandidateDetailed> exZbcandidateDetaileds = exZbcandidateDetailedService.selectByTenderListIds(tenderListId.toArray(new String[0]));
        List<ExTbcompanyScoreCandidate> exTbcompanyScoreCandidateList = new ArrayList<>();
        for (int i = 0; i < page1.getRecords().size(); i++) {
            ExTbcompanyScore item = page1.getRecords().get(i);
            ExTbcompanyScoreCandidate exTbcompanyScoreCandidate = new ExTbcompanyScoreCandidate();
            BeanUtils.copyProperties(item, exTbcompanyScoreCandidate);
            exTbcompanyScoreCandidate.setIsCandidate(false);
            if (CollectionUtil.isNotEmpty(exZbcandidateDetaileds)) {
                Optional<ExZbcandidateDetailed> exZbcandidateDetailedOptional = exZbcandidateDetaileds.stream().filter(obj -> obj.getTenderListId().equals(item.getTenderListId())).findFirst();
                if (exZbcandidateDetailedOptional.isPresent() && exZbcandidateDetailedOptional.get() != null) {
                    exTbcompanyScoreCandidate.setIsCandidate(true);
                    exTbcompanyScoreCandidate.setExZbcandidateDetailedId(exZbcandidateDetailedOptional.get().getId());
                }
            }
            exTbcompanyScoreCandidateList.add(exTbcompanyScoreCandidate);
        }
        page1.setRecords(exTbcompanyScoreCandidateList);
        return Result.OK(page1);
    }

    /**
     * 添加
     *
     * @param exTbcompanyScore
     * @return
     */
    @AutoLog(value = "投标单位得分-添加")
    @ApiOperation(value = "投标单位得分-添加", notes = "投标单位得分-添加")
    //@RequiresPermissions("org.jeecg.modules:ex_tbcompany_score:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ExTbcompanyScore exTbcompanyScore) {
        exTbcompanyScoreService.save(exTbcompanyScore);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param exTbcompanyScore
     * @return
     */
    @AutoLog(value = "投标单位得分-编辑")
    @ApiOperation(value = "投标单位得分-编辑", notes = "投标单位得分-编辑")
    //@RequiresPermissions("org.jeecg.modules:ex_tbcompany_score:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ExTbcompanyScore exTbcompanyScore) {
        exTbcompanyScoreService.updateById(exTbcompanyScore);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "投标单位得分-通过id删除")
    @ApiOperation(value = "投标单位得分-通过id删除", notes = "投标单位得分-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:ex_tbcompany_score:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        exTbcompanyScoreService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "投标单位得分-批量删除")
    @ApiOperation(value = "投标单位得分-批量删除", notes = "投标单位得分-批量删除")
    //@RequiresPermissions("org.jeecg.modules:ex_tbcompany_score:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.exTbcompanyScoreService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "投标单位得分-通过id查询")
    @ApiOperation(value = "投标单位得分-通过id查询", notes = "投标单位得分-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ExTbcompanyScore> queryById(@RequestParam(name = "id", required = true) String id) {
        ExTbcompanyScore exTbcompanyScore = exTbcompanyScoreService.getById(id);
        if (exTbcompanyScore == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(exTbcompanyScore);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param exTbcompanyScore
     */
    //@RequiresPermissions("org.jeecg.modules:ex_tbcompany_score:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExTbcompanyScore exTbcompanyScore) {
        return super.exportXls(request, exTbcompanyScore, ExTbcompanyScore.class, "投标单位得分");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("ex_tbcompany_score:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ExTbcompanyScore.class);
    }
    @ApiOperation(value = "投标单位得分-通过id查询", notes = "投标单位得分-通过id查询")
    @GetMapping(value = "/setCandidate")
    public Result<Boolean> setCandidate(@RequestParam(name = "id", required = true) String id) {
        return Result.OK(exTbcompanyScoreService.setCandidate(id));
    }

}
